<?php 
class NewsAdminCats extends Module{
	function NewsAdminCats($row){
		Module::Module($row);
		
		if (!User::have_permit(ADMIN_NEWS))
		Url::access_denied();
		
		switch(Url::get('cmd'))
		{
			case 'delete':		
				$this->add_form(new DeleteNewsCatForm());		
				break;
			case 'move_down':		
			case 'move_up':		
				$this->add_form(new MoveNewsCatForm());		
				break;
			case 'move_home':	
			case 'move_end':	
					$this->add_form(new MoveHomeNewsCatForm());	
					break;
					
			case 'edit':		
				require_once 'forms/NewsAdminCatsEdit.php';
				$this->add_form(new NewsAdminCatsEditForm());		
				break;
			case 'add':			
				require_once 'forms/NewsAdminCatsAdd.php';
				$this->add_form(new NewsAdminCatsAddForm());			
				break;		
			default:			
				require_once 'forms/NewsAdminCats.php';
				$this->add_form(new NewsAdminCatsForm()); 
		}
	}
}


class DeleteNewsCatForm extends Form
{
	function __construct()
	{				
		Form::Form('NewsAdminCatsForm');
	
		$id = Url::get('id',0);
		
		if($id)
		{
			$row = DB::select('news_categories',"id=$id");
			
			if($row)
			{
				DB::delete('news_categories',"id=".$row['id']);
				DB::query("UPDATE news_categories SET position=position-1 WHERE id!=".$row['id']." AND position>=".$row['position']);
				
				AZLib::getNewsCats(1);
			}
		}

		Url::redirect_current();
	}
}

class MoveNewsCatForm extends Form
{
	function __construct()
	{
		global $oDB;
		Form::Form('MoveNewsCatForm');
		
		$catid = Url::get('id',0);
		
		if($catid)
		{
			$category=DB::select('news_categories',$catid);
			
			$cmd=Url::get('cmd');
			
			if($category)
			{
				if($cmd=='move_up')
				{
					if($category['position']>1)
					{
						DB::query("UPDATE news_categories SET position=position-1 WHERE id=".$category['id']);
						
						if(DB::affected_rows())
						DB::query("UPDATE news_categories SET position=position+1 WHERE id!=".$category['id']." AND position=".($category['position']-1));
					}
				}
				else
				{
					if($category['position'] < DB::count("news_categories"))
					{
						DB::query("UPDATE news_categories SET position=position+1 WHERE id=".$category['id']);
						
						if(DB::affected_rows())
						DB::query("UPDATE news_categories SET position=position-1 WHERE id!=".$category['id']." AND position=".($category['position']+1));
					}
				}
				
				AZLib::getNewsCats(1);
			}
		}
		
		Url::redirect_current();
	}
}

class MoveHomeNewsCatForm extends Form
{
	function __construct()
	{
		Form::Form('MoveHomeNewsCatForm');
		
		$catid		=	Url::get('id',0);
		
		if($catid)
		{
			$cmd		=	Url::get('cmd');
			$category	=	DB::select('news_categories',$catid);
			
			if($category)
			{
				if($cmd=='move_home')
				{
					DB::query("UPDATE news_categories SET position = 1 WHERE id={$category['id']}");
					DB::query("UPDATE news_categories SET position = position+1 WHERE id!={$category['id']} AND position>=1 AND position<={$category['position']}");
				}
				else
				{
					$posMax	=	DB::count("news_categories");
					
					DB::query("UPDATE news_categories SET position = $posMax WHERE id={$category['id']}");
					
					DB::query("UPDATE news_categories SET position = position-1 WHERE id!={$category['id']} AND position>={$category['position']} AND position<=$posMax");
				}
				
				AZLib::getNewsCats(1);
			}
		}
		
		Url::redirect_current();
	}
}
?>